<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Class BooksController
 * @package Admin\Controller
 * 作用：书籍管理
 */
class BooksController extends Controller{
    //申明数据库变量
    private $book;

    public function __construct(){
        parent::__construct();
        //实例化数据库表
        $this->book=M('Book');
    }
     
    /**
     * 
     */
    public function book_cata_list(){
        if(IS_GET){
            $type=I('get.level_one',1);
            $result=D('Book')->getCata($type);
            $CataOne=$this->getCataOne();
            $this->assign('CataOne',$CataOne);
            $this->assign('level_one_hid',$type);
            $this->assign('cata_list',$result['list']);
            $this->assign('page',$result['show']);
            $this->display('Book_Cata_list');
        }
    
    }
    
    
    public function getCataOne(){
        $result=M('category')->where('level=1')->select();
        if($result){
            return $result;
        }
    }
    
    
    
    /**
     * @action index
     * 作用：书籍列表
     */
    public function Book_list(){
          $re=I('get.');
          $map['cata_id']=$re['cat_two'];
          $bookList=$this->book->where($map)->select();
          $this->assign('cata_id',$re['cat_two']);
          $this->assign('book_List',$bookList);
          $this->display();
    }

    /**
     * @ation addbook
     * 作用：添加书籍
     */
    public function addBook(){
        if($_POST){
            //组装插入书籍
            $data=array(
                'name'=>$_POST['name'],
                'cover'=>$_POST['cover'],
                'about'=>$_POST['about'],
                'price'=>$_POST['price'],
                'minus'=>$_POST['minus'],
                'number'=>$_POST['number'],
                'cata_id'=>$_POST['cata_id'],
                'addtime'=>time(),
            );
            $cata_id=$_POST['cata_id'];
            //往数据库添加书籍
            if($this->book->add($data)){
                $this->success('添加成功',U('Book_list',['cat_two'=>$cata_id]));
            }else{
                $this->success('添加失败');
            }
        }else{
            $cata_id=$_GET['cata_id'];
            $this->assign('cata_id',$cata_id);
            $this->display('add');
        }
    }

    /**
     * @action editBooks
     * 作用：编辑书籍
     */
    public function editBooks(){
        if($_POST){
            //组装编辑书籍
            $data=array(
                'id'=>$_GET['id'],
                'name'=>$_POST['name'],
                'cover'=>$_POST['cover'],
                'about'=>$_POST['about'],
                'price'=>$_POST['price'],
                'minus'=>$_POST['minus'],
                'number'=>$_POST['number'],
                'cata_name'=>$_POST['cata_name'],
                'cata_id'=>$_POST['cata_id'],
                'addtime'=>time(),
            );
           $cata_id=$_POST['cata_id'];
            //编辑书籍操作
            if($this->book->save($data)>=0){
                $this->success('编辑成功',U('Book_list',['cat_two'=>$cata_id]));
            }else{
                $this->success('编辑失败');
            }
        }else{
            //查询对应书籍内容
            $this->assign('bookli',$this->book->where('id='.$_GET['id'])->find());
            $this->display('edit');
        }
    }

    /**
     * @action delBooks
     * 作用： 单个删除书籍
     */
    public function delBooks(){
        //删除操作
        if($this->book->where('id='.$_GET['id'])->delete()){
            $this->success('删除成功');
        }else{
            $this->success('删除失败');
        }
    }

    /**
     * @action allDel
     * 作用：批量删除书籍
     */
    public function delete_all(){
        if(IS_GET){
            $info=I('get.');
            if(!empty($info)){
                $re=explode('@',$info['ids']);
                if(!empty($re)){
                    $map['id']=array('in',$re);
                    $result=M('book')->where($map)->delete();
                    $result=$result?true:false;
                    $this->ajaxReturn($result);
                }
                else{
                    $this->ajaxReturn(false);
                }
            }else{
                $this->ajaxReturn('提交信息出错');
            }
        }
    }
    
    /**
     * @action allDel
     * 作用：设置二级目录下面图书的运费
     */
    
    public function setPostage(){
        if(IS_GET){
            $cat_id=I('get.cata_id');
            $postage=M('price')->where(['cate_id'=>$cat_id])->find();
            if(empty($postage)){
                $data['cate_id']=$cat_id;
                $re=M('price')->add($data);
                if(!$re){
                    $this->error('初始化数据失败');
                }else{
                    $fee=M('price')->where(['cate_id'=>$cat_id])->field('postage')->find();
                    $this->assign('cate_id',$cat_id);
                    $this->assign('postage',$fee['postage']);
                    $this->display();
                }
            }else{
                $fee=M('price')->where(['cate_id'=>$cat_id])->field('postage')->find();
                $this->assign('cate_id',$cat_id);
                $this->assign('postage',$fee['postage']);
                $this->display();
            }
        }else{
            $fee=I('post.postage');
            $cate_id=I('post.cate_id');
            $data['postage']=$fee;
            $fee=M('price')->where(['cate_id'=>$cate_id])->save($data);
            if($fee){
                $this->success('修改邮费成功',U('Book_list',['cat_two'=>$cate_id]));
            }else{
                $this->error('未做任何修改');
            }
        }
    }
}